package com.taysear.bus;

import com.taysear.dbservice.DbServicesPrx;
import com.taysear.ice.DbServiceCommunicator;
import com.vaadin.Application;
import com.vaadin.Application.CustomizedSystemMessages;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

/**
 * <code>BusApplication</code> is the application main class.
 * 
 * @author Abdullahi
 */
@SuppressWarnings("serial")
public class BusApplication extends Application {
	
	private static String LOGOUT_URL = "/";
    private static String APP_CONTEXT_PATH = "";
    private static String FULL_LOGOUT_URL = "";
    
	MainLayout mainLayout;
	
	@Override
	public void init() {
		Window mainWindow = new Window("Bus portal");
		
		mainLayout = new MainLayout();
		mainWindow.addComponent(mainLayout);
		
		// Create the application data instance
        AppData sessionData = new AppData(this);
        
        // Register it as a listener in the application context
        getContext().addTransactionListener(sessionData);
        
        APP_CONTEXT_PATH = ((WebApplicationContext)getContext()).getHttpSession().getServletContext().getContextPath();
		FULL_LOGOUT_URL = APP_CONTEXT_PATH + LOGOUT_URL;
		setLogoutURL(FULL_LOGOUT_URL);
		
		setMainWindow(mainWindow);
		
		setTheme("bustheme");
	}
	
	/**
     * Gets the SystemMessages for this application. SystemMessages are used to
     * notify the user of various critical situations that can occur, such as
     * session expiration, client/server out of sync, and internal server error.
     * 
     * You can customize the messages by "overriding" this method and returning
     * {@link CustomizedSystemMessages}. To "override" this method, re-implement
     * this method in your application (the class that extends
     * {@link Application}). Even though overriding static methods is not
     * possible in Java, Vaadin selects to call the static method from the
     * subclass instead of the original {@link #getSystemMessages()} if such a
     * method exists.
     * 
     * @return the SystemMessages for this application
     */
   	public static SystemMessages getSystemMessages() {
   		CustomizedSystemMessages m = new CustomizedSystemMessages();
   		m.setAuthenticationErrorNotificationEnabled(false);
   		m.setAuthenticationErrorURL(FULL_LOGOUT_URL);
   		m.setCommunicationErrorNotificationEnabled(false);
   		m.setCommunicationErrorURL(FULL_LOGOUT_URL);
   		m.setCookiesDisabledNotificationEnabled(false);
   		m.setCookiesDisabledURL(FULL_LOGOUT_URL);
   		m.setInternalErrorNotificationEnabled(false);
   		m.setInternalErrorURL(FULL_LOGOUT_URL);
   		m.setOutOfSyncNotificationEnabled(false);
   		m.setOutOfSyncURL(FULL_LOGOUT_URL);
   		m.setSessionExpiredNotificationEnabled(false);
   		m.setSessionExpiredURL(FULL_LOGOUT_URL);
   		return m;
   	}
	
	/**
	 * Gets the mainlayout of the application.
	 * 
	 * @return MainLayout
	 */
	public MainLayout getMainLayout() {
		return mainLayout;
	}
	
	/**
     * Shows a error notification message the window.
     * 
     * @param pMessage
     */
	public void showErrorNotification(String pMessage) {
		getMainWindow().showNotification(pMessage, Notification.TYPE_WARNING_MESSAGE);
	}
	
	/**
     * Shows a simple notification message the window.
     * 
     * @param pMessage
     */
	public void showNotification(String pMessage) {
		getMainWindow().showNotification(pMessage);
	}
	
	/**
	 * Gets a proxy that is used to communicate with the db service module.
	 * 
	 * @return DbServicesPrx
	 */
	public DbServicesPrx getDbServiceProxy() {
		return DbServiceCommunicator.getDbServiceProxy();
	}
}
